package com.leetcode.q423;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p> description: desc</p>
 *
 * @author tanzc
 * @date 2022/3/30
 **/
public class OriginalDigits {

    public String originalDigitss(String s) {
        // ["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]
        int[] num = new int[10];
        int c0 = 0, c1 =0 ,c2 = 2, c3 = 0, c4 = 0, c5 = 0, c6 = 0, c7 = 0, c8 = 0, c9 = 0;
        for (int i = 0; i < s.length(); i++) {
            switch (s.charAt(i)) {
                case 'z':
                    num[0]++;
                    break;
                case 'o':
                    num[1]++;
                    break;
                case 'w':
                    num[2]++;
                    break;
                case 't':
                    num[3]++;
                    break;
                case 'u':
                    num[4]++;
                    break;
                case 'f':
                    num[5]++;
                    break;
                case 'x':
                    num[6]++;
                    break;
                case 's':
                    num[7]++;
                    break;
                case 'g':
                    num[8]++;
                    break;
                case 'i':
                    num[9]++;
                    break;
                default:
                    break;
            }
        }
        num[3] -= (num[2] + num[8]);
        num[5] -= num[4];
        num[7] -= num[6];
        num[1] -= (num[2] + num[0] + num[4]);
        num[9] -= (num[5] + num[6] + num[8]);
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num.length; i++) {
            for (int j = 0; j < num[i]; j++) {
                sb.append(i);
            }
        }
        return sb.toString();
    }

    public String originalDigits(String s) {
        List<Integer> list = new ArrayList<>();
        StringBuffer ss = new StringBuffer(s);
        // zero
        while (ss.indexOf("z") > -1) {
            list.add(0);
            ss.deleteCharAt(ss.indexOf("z")).
                    deleteCharAt(ss.indexOf("e"))
                    .deleteCharAt(ss.indexOf("r"))
                    .deleteCharAt(ss.indexOf("o"));
        }
        // two
        while (ss.indexOf("w") > -1) {
            list.add(2);
            ss.deleteCharAt(ss.indexOf("t"))
                    .deleteCharAt(ss.indexOf("w"))
                    .deleteCharAt(ss.indexOf("o"));
        }
        // eight
        while (ss.indexOf("g") > -1) {
            list.add(8);
            ss.deleteCharAt(ss.indexOf("e"))
                    .deleteCharAt(ss.indexOf("i"))
                    .deleteCharAt(ss.indexOf("g"))
                    .deleteCharAt(ss.indexOf("h"))
                    .deleteCharAt(ss.indexOf("t"));
        }
        // six
        while (ss.indexOf("x") > -1) {
            list.add(6);
            ss.deleteCharAt(ss.indexOf("s"))
                    .deleteCharAt(ss.indexOf("i"))
                    .deleteCharAt(ss.indexOf("x"));
        }
        // three
        while (ss.indexOf("t") > -1) {
            list.add(3);
            ss.deleteCharAt(ss.indexOf("t"))
                    .deleteCharAt(ss.indexOf("h"))
                    .deleteCharAt(ss.indexOf("r"))
                    .deleteCharAt(ss.indexOf("e"))
                    .deleteCharAt(ss.indexOf("e"));
        }
        // four
        while (ss.indexOf("u") > -1) {
            list.add(4);
            ss.deleteCharAt(ss.indexOf("f"))
                    .deleteCharAt(ss.indexOf("o"))
                    .deleteCharAt(ss.indexOf("u"))
                    .deleteCharAt(ss.indexOf("r"));
        }
        // five
        while (ss.indexOf("f") > -1) {
            list.add(5);
            ss.deleteCharAt(ss.indexOf("f"))
                    .deleteCharAt(ss.indexOf("i"))
                    .deleteCharAt(ss.indexOf("v"))
                    .deleteCharAt(ss.indexOf("e"));
        }
        // one
        while (ss.indexOf("o") > -1) {
            list.add(1);
            ss.deleteCharAt(ss.indexOf("o"))
                    .deleteCharAt(ss.indexOf("n"))
                    .deleteCharAt(ss.indexOf("e"));
        }
        // seven
        while (ss.indexOf("s") > -1) {
            list.add(7);
            ss.deleteCharAt(ss.indexOf("s"))
                    .deleteCharAt(ss.indexOf("e"))
                    .deleteCharAt(ss.indexOf("v"))
                    .deleteCharAt(ss.indexOf("e"))
                    .deleteCharAt(ss.indexOf("n"));
        }
        // nine
        while (ss.indexOf("n") > -1) {
            list.add(9);
            ss.deleteCharAt(ss.indexOf("n"))
                    .deleteCharAt(ss.indexOf("i"))
                    .deleteCharAt(ss.indexOf("n"))
                    .deleteCharAt(ss.indexOf("e"));
        }

        return list.stream().sorted().map(Object::toString).collect(Collectors.joining());
    }
}
